/*
 * Copyright (C), 2018-2019, 深圳联合保理有限公司
 * FileName: RedisKeyUtil
 * Author:   jinjie.chen
 * Date:     2019/9/6 18:21
 * @since 1.0.0
 */
package com.dome.util.redis;


import com.dome.util.constans.RedisKeyConstants;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author jinjie.chen
 * 2019/9/6 18:21
 * @since 1.0.0
 */
public class RedisKeyUtil {
    public static final String DOMAIN_ID = "Test";
    /**
     * 登陆用户缓存key
     * @param account 账号
     * @param sessionId sessionid
     * @return
     */
    public static String loginRedisKey(String account,String sessionId) {
        return RedisKeyConstants.SESSIONID+account+":"+sessionId;
    }
    /**
     * 登陆用户缓存key
     * @param request 请求request
     * @param account 账号
     * @return
     */
    public static String loginRedisKey(HttpServletRequest request, String account) {
        HttpSession httpSession = request.getSession(true);
        String sessionId = httpSession.getId();
        return RedisKeyConstants.SESSIONID+account+":"+sessionId;
    }
    /**
     * 登陆用户缓存key 列表 一个用户可能多个人登陆
     * @param account 账号
     * @return
     */
    public static String loginRedisKeyList( String account) {

        return RedisKeyConstants.SESSIONID+account+":*";
    }

    /**
     * 租户权限
     * @param domainId
     * @return
     */
    public static String accessResourceTreeRedisKeyList(String domainId) {
        if(domainId==null) {
            domainId = DOMAIN_ID;
        }
        String accessResourceTreeRedisKey = RedisKeyConstants.ACCESS_RESOURCE_TREE + domainId + ":*";
        return accessResourceTreeRedisKey;
    }
    /**
     * 租户账号权限
     * @param domainId
     * @return
     */
    public static String accessResourceTreeRedisKey(String domainId,Long account) {
        if(domainId==null) {
            domainId = DOMAIN_ID;
        }
        String accessResourceTreeRedisKey = RedisKeyConstants.ACCESS_RESOURCE_TREE
                + domainId
                + ":" +account;
        return accessResourceTreeRedisKey;
    }
    public static String accessResourceTreeDomainRedisKey(String domainId) {
        if(domainId==null) {
            domainId = DOMAIN_ID;
        }
        String accessResourceTreeRedisKey = RedisKeyConstants.ACCESS_RESOURCE_TREE
                + domainId;
        return accessResourceTreeRedisKey;
    }

    public static String openAccessResourceTreeRedisKey(String domainPrimaryId,Long account) {
        if(domainPrimaryId==null) {
            domainPrimaryId = "10000";
        }
        String accessResourceTreeRedisKey = RedisKeyConstants.OPEN_ACCESS_RESOURCE_TREE
                + domainPrimaryId
                + ":" +account;
        return accessResourceTreeRedisKey;
    }

}